package com.coolingme.springframework.context.support;

import com.coolingme.springframework.beans.config.MeBeanDefinition;
import com.coolingme.springframework.core.MeBeanFactory;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * org.springframework.beans.factory.support.DefaultListableBeanFactory
 *
 * @author wangyue
 * @date 2024/08/29 19:19
 */
public class MeDefaultListableBeanFactory implements MeBeanFactory {


    /**
     * 存储注册信息的容器
     * key ：beanDefinition的factoryBeanName（bean名称），类名小写，接口为全类名
     * value：beanDefinition对象本身
     * <p>
     * "demoServiceImpl" -> {MeBeanDefinition@5122} "MeBeanDefinition{factoryBeanName='demoServiceImpl', beanClassName='com.coolingme.demo.service.impl.DemoServiceImpl'}"
     * "com.coolingme.demo.service.DemoService" -> {MeBeanDefinition@5123} "MeBeanDefinition{factoryBeanName='com.coolingme.demo.service.DemoService', beanClassName='com.coolingme.demo.service.impl.DemoServiceImpl'}"
     * "demoController" -> {MeBeanDefinition@5121} "MeBeanDefinition{factoryBeanName='demoController', beanClassName='com.coolingme.demo.controller.DemoController'}"
     */
    public Map<String, MeBeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>();

    @Override
    public Object getBean(String beanName) {
        return null;
    }

    @Override
    public <T> T getBean(Class<T> requiredType) {
        return null;
    }


    /**
     * 缓存配置文件信息
     *
     * @param beanDefinitions 配置文件信息
     */
    public void doRegisterBeanDefinition(List<MeBeanDefinition> beanDefinitions) throws Exception {
        for (MeBeanDefinition beanDefinition : beanDefinitions) {
            // 存在重复的key，直接抛异常
            if (this.beanDefinitionMap.containsKey(beanDefinition.getFactoryBeanName())) {
                throw new Exception("The is" + beanDefinition.getFactoryBeanName() + " exist");
            }
            // key不同，对象是同一个
            beanDefinitionMap.put(beanDefinition.getFactoryBeanName(), beanDefinition);
        }
    }


}
